package com.example.springboot0.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.stream.Stream;

@Component
@Aspect
public class MyAspect {

    /**
     * * 一个 。。多个
     * 任意返回值
     */
//    @Pointcut("execution(* com.example.springboot0.service.imp.*Imp.*(..))")
    @Pointcut("execution(* com.*..*.*Imp.*(..))")
    public void bbb() {

    }

//    @Pointcut("@annotation(com.example.springboot0.aspect.Log)")
//    public void ccc() {
//
//    }


    @Around("@annotation(aaalog)")
    public Object aroundLog(ProceedingJoinPoint joinPoint, Log aaalog) {
        FileOutputStream out = null;
        Object o = null;
        try {
//
//            Signature signature = joinPoint.getSignature();
//            MethodSignature methodSignature = (MethodSignature) signature;
//            Method method = methodSignature.getMethod();
//
//
//            out = new FileOutputStream("D:/a.log", true);
//            Method[] ms = joinPoint.getTarget().getClass().getDeclaredMethods();
//            StringBuffer sb = new StringBuffer();
//            for (int i = 0; i < ms.length; i++) {
//                if (ms[i].getName().equals(method.getName()))
//                    Stream.of(ms[i].getDeclaredAnnotations()).forEach(item -> {
//                        if (item instanceof Log) {
//                            Log log = (Log)item;
//                            sb.append(log.value());
//                            sb.append("\r\n");
//                        }
//                    });
//            }
            out = new FileOutputStream("D:/a.log", true);
            out.write(aaalog.value().toString().getBytes());
            o = joinPoint.proceed();
            out.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }


        return o;
    }


    @Before("bbb()")
    public void befor() {
        System.out.println("-----加钱1------");
    }

    @Around("bbb()")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object o = null;
        try {
            System.out.println("---Around befor 加1块-----");
            o = joinPoint.proceed();
            System.out.println("---Around after 加1块-----");
        } catch (Throwable e) {
            System.out.println("---Around exception 加1块-----");
        } finally {
            System.out.println("---Around finally 加1块-----");
        }
        return o;
    }
}
